package org.rsbot.event.impl;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;

import org.rsbot.bot.Bot;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Methods;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;
import org.rsbot.script.Calculations;
import org.rsbot.script.wrappers.RSItemTile;

public class DrawItems implements PaintListener {

    private Methods ctx;

    public DrawItems(Bot bot) {
        ctx = bot.getMethods();
    }

    public void onRepaint(final Graphics render) {
        if (!ctx.isLoggedIn()) {
            return;
        }
        final RSPlayer player = ctx.getMyPlayer();
        if (player == null) {
            return;
        }
        final FontMetrics metrics = render.getFontMetrics();
        final RSTile location = player.getLocation();
        final int locX = location.getX();
        final int locY = location.getY();
        final int tHeight = metrics.getHeight();
        for (int x = locX - 25; x < locX + 25; x++) {
            for (int y = locY - 25; y < locY + 25; y++) {
                final Point screen = Calculations.tileToScreen(new RSTile(x, y));
                if (!ctx.pointOnScreen(screen)) {
                    continue;
                }
                final RSItemTile[] items = ctx.getGroundItemsAt(x, y);
                for (int i = 0; i < items.length; i++) {
                    render.setColor(Color.RED);
                    render.fillRect((int) screen.getX() - 1, (int) screen.getY() - 1, 2, 2);
                    final String s = "ID : [ " + items[i].getItem().getID() + " ]";
                    final int ty = screen.y - tHeight * (i + 1) + tHeight / 2;
                    final int tx = screen.x - metrics.stringWidth(s) / 2;
                    render.setColor(Color.green);
                    render.drawString(s, tx, ty);
                }
            }
        }
    }
}
